import request, { RequestDto, PageData, PageParams } from './request'
import qs from 'qs'

export interface ArticleDto {
  _id: string
  title: string
  excerpt: string
  content: string
  image?: {
    _id: string
    url: string
    createdAt: string
  }
  author: {
    _id: string
    email: string
    nickName: string
    description: string
    avatar: string
    column: string
    createdAt: string
  }
  column: string
  createdAt: string
}
export interface PatchArticle {
  title: string
  content: string
  image: string
}
export interface PostArticle extends PatchArticle {
  column: string
  author: string
}

// 新建文章
export function createArticle(data: PostArticle): Promise<RequestDto<ArticleDto>> {
  return request.post('/posts', data)
}
// 获取对应专栏的文章列表
export function getArticlesByColumnId(
  id: string,
  pageParams: PageParams
): Promise<RequestDto<PageData<ArticleDto>>> {
  return request.get(`/columns/${id}/posts?${qs.stringify(pageParams)}`)
}
// 获取文章详情
export function getArticleDetail(id: string): Promise<RequestDto<ArticleDto>> {
  return request.get(`/posts/${id}`)
}
// 删除文章
export function deleteArticle(id: string): Promise<RequestDto<ArticleDto>> {
  return request.delete(`/posts/${id}`)
}
// 更新文章
export function updateArticle(id: string, data: PatchArticle): Promise<RequestDto<ArticleDto>> {
  return request.patch(`/posts/${id}`, data)
}
